import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:largefriends/res.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/lf_text_style.dart';

import 'keyboard_height.dart';

class LfTextFiled extends StatefulWidget {
  final TextEditingController textEditingController;
  final ValueChanged<String>? onChanged;
  final bool isPassword;
  final TextInputAction? textInputAction;
  final FocusNode? focusNode;
  final int? minLine;
  final int? maxLine;
  final double borderRadius;
  final TextStyle? inputStyle;
  final TextStyle? hintStyle;
  final String hintText;
  final List<TextInputFormatter>? inputFormatters;
  final TextInputType? keyboardType;
  final bool? enable;
  const LfTextFiled(
      {super.key,
      required this.textEditingController,
      this.onChanged,
      required this.isPassword,
      this.textInputAction,
      this.focusNode,
      this.minLine,
      this.maxLine,
      this.inputStyle,
      this.hintStyle,
      this.borderRadius = 5,
      required this.hintText,
      this.inputFormatters,
      this.keyboardType,
      this.enable = true});

  @override
  State<LfTextFiled> createState() => _LfTextFiledState();
}

class _LfTextFiledState extends State<LfTextFiled> with
    WidgetsBindingObserver,
    KeyboardHeight,
    SingleTickerProviderStateMixin{
  bool showPassword = false;
  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: widget.borderRadius.r.radius,
      child: TextField(
        autofocus: false,
        enabled: widget.enable,
        inputFormatters: widget.inputFormatters,
        style: widget.inputStyle ??
            lfTextStyle(sp: 14.sp, fontFamily: fontRegular),
        cursorColor: colorFEFEFE,
        keyboardType: widget.keyboardType ?? TextInputType.text,
        focusNode: widget.focusNode,
        obscureText: widget.isPassword && !showPassword,
        controller: widget.textEditingController,
        onChanged: widget.onChanged,
        textInputAction: widget.textInputAction,
        maxLines: widget.maxLine ?? 1,
        minLines: widget.minLine ?? 1,
        decoration: InputDecoration(
            isCollapsed: true,
            fillColor: color8F8F8F,
            filled: true,
            hintText: widget.hintText,
            hintStyle: widget.hintStyle ?? lfHintStyle,
            contentPadding:
                EdgeInsets.symmetric(vertical: 12.h, horizontal: 18.w),
            suffixIcon: widget.isPassword
                ? InkWell(
                    splashColor: Colors.transparent,
                    highlightColor: Colors.transparent,
                    onTap: () {
                      setState(() {
                        showPassword = !showPassword;
                      });
                    },
                    child: Container(
                      height: 40,
                      width: 40,
                      alignment: Alignment.center,
                      child: Image.asset(!showPassword
                          ? Res.login_eye_close
                          : Res.login_eye_open),
                    ),
                  )
                : const SizedBox.shrink(),
            focusedBorder: null,
            enabledBorder: null,
            border: InputBorder.none),
      ),
    );
  }
}
